<template>
	<!--pages/Collage/Collage.wxml-->
	<block v-if="goods">
		<view class="many-list">
			<view class="many-image">
				<image mode="aspectFit" :src="manyArr.goodsimg"></image>
			</view>
			<view class="many-box">
				<view class="manyName">{{manyArr.goodsname}}</view>
				<view class="manyPrice">
					<text class="PresentPrice">￥{{manyArr.pricea}}</text>
					<text class="OriginalPrice">￥{{manyArr.priceb}}</text>
				</view>
				<view class="tabel-group">
					<view class="menyTabel">{{num}}人团</view>
					<view class="menyTabel">拼团立省{{manyArr.province}}元</view>
				</view>
			</view>
		</view>
		<!-- 邀请参团 -->
		<view class="invite">
			<view class="invite-manyWrap">
				<image src="/static/images/manyBg.png"></image>
				<view class="invite-Wrap">
					<block v-if="number_s > 0">
						<view class="participate">已有{{member_num}}人参与，还差{{number_s}}人拼团成功</view>
						<view class="Remaining" v-if="Remaining && Remaining != '0'">仅剩{{Remaining}}失效</view>
						<view class="Remaining" v-else>已截止</view>
					</block>
					<block v-else>
						<view class="participate">拼团成功</view>
						<view class="Remaining">已成功</view>
					</block>
					<view class="participate-box">
						<view class="chief" v-for="(item, index) in member" :key="index">
							<image :src="item.avatar"></image>
							<view class="chiefImg" v-if="item.parent_id && item.parent_id == item.id">
								<image src="/static/images/chief.png"></image>
							</view>
						</view>
						<view class="chief" v-if="number_s && number_s > 0" v-for="(item, index) in number_s"
							:key="index">
							<image src="https://safood.supplyocta.com/Public/MallSmallApp/lack.png"></image>
						</view>
					</view>
				</view>
			</view>
		</view>
		<view class='group-title-border'>
			<block v-if="ty==1">
				<button type="warn" open-type="share" class="weui-btn invite-group" v-if="number_s > 0">邀请参团</button>
				<button type="warn" class="weui-btn success-group" v-else>已成功</button>
			</block>
			<block v-else>
				<button type="warn" class="weui-btn confirm-group" @tap="determine">确定参团</button>
			</block>
		</view>
	</block>
</template>

<script>
	// pages/Collage/Collage.js
	var app = getApp().globalData;

	export default {
		data() {
			return {
				manyArr: {},
				invite: {
					Remaining: "0"
				},
				Remaining: '0',
				endData: "" // 拼团结束时间
					,
				order_id: "",
				type: "",
				ty: "",
				num: "",
				member_num: "",
				member: "",
				user_id: "",
				actid: "",
				number_s: "",
				goods: 0
			};
		},

		components: {},
		props: {},
		// 加载页面，接受数据
		onLoad(options) {
			// #ifdef MP-ALIPAY
			if (JSON.stringify(options) == '{}') {
				options = my.getLaunchOptionsSync().query;
			}
			// #endif
			if (options && (options.store_id || options.storeid)) {
				this.$store.store_id = options.store_id ? options.store_id : options.storeid;
			}

			this.setData({
				order_id: options.order_id,
				type: options.type,
				ty: options.ty
			});
		},
		// 加载页面，获取数据
		async onShow() {
			await this.$onLaunched;
			this.getShowGroup();
		},
		// 页面卸载
		onUnload() {
			if (this.type == 1) {
				uni.redirectTo({
					url: '/pages/myOrder/myOrder'
				})
			}
		},
		// 用户分享
		onShareAppMessage(res) {
			var act_name = this.manyArr.goodsname;
			var order_id = this.order_id;
			return {
				title: act_name,
				path: '/pages/index/Collage/Collage?order_id=' + order_id + '&store_id=' + this.$store.store_id + '&ty=2&type=0' + '&share_uid=' + app.XcxData.userInfo.uid
			};
		},
		methods: {
			// 获取展示数据
			getShowGroup() {
				uni.showLoading({
					title: '加载中...',
					// #ifndef MP-ALIPAY
					mask: true,
					// #endif
				});
				var that = this;
				getApp().globalData.sendRequest({
					url: '/Group/getShowGroup',
					type: 'post',
					data: {
						bill_id: that.order_id
					},

					success(res) {
						uni.hideLoading();

						if (res.status == 1) {
							if (res.group_user[0].parent_id == 0) {
								that.setData({
									order_id: res.group_user[0].id
								});
							} else {
								that.setData({
									order_id: res.group_user[0].parent_id
								});
							}
							let manyArr = {
								goodsimg: res.group_data.image,
								goodsname: res.group_data.act_name,
								pricea: res.group_data.act_price,
								priceb: res.group_data.online_price,
								province: res.group_data.province,
							}
							that.setData({
								manyArr: manyArr,
								endData: res.bill_order.end_date,
								num: res.group_data.number,
								member_num: res.join_number,
								member: res.group_user,
								user_id: res.bill_order.uid,
								actid: res.bill_order.group_id,
								number_s: res.number_s,
								goods: 1,
							});
							console.log(that.manyArr)
						} else {
							uni.showModal({
								title: '提示',
								content: res.msg,
								showCancel: false,
								confirmText: '确定',
								confirmColor: '#fd633a',
								success: function(res) {
									if (res.confirm) {
										uni.redirectTo({
											url: '../../myOrder/myOrder'
										});
									}
								}
							});
						}

						that.countdown();
					}

				});
			},

			// 立即开团
			todetail: function(e) {
				var id = e.currentTarget.dataset.id;
				uni.navigateTo({
					url: '../Group-purchase-details/Group-purchase-details?id=' + id
				});
			},

			//倒计时
			countdown() {
				var invite = this.invite,
					endData = this.endData,
					Remaining = invite.Remaining,
					curDate = new Date().getTime(),
					count = endData - curDate;
				var that = this;
				var day, Time, Minute, second, millisecond;

				if (count >= 0) {
					day = Math.floor(count / 1000 / 60 / 60 / 24); //天

					Time = Math.floor(count / 1000 / 60 / 60 % 24); //时

					Minute = Math.floor(count / 1000 / 60 % 60); //分

					second = Math.floor(count / 1000 % 60); //秒

					millisecond = Math.floor(count % 1000); //毫秒

					millisecond = millisecond < 100 ? '0' + millisecond : millisecond;
					second = second < 10 ? '0' + second : second;
					Minute = Minute < 10 ? '0' + Minute : Minute;
					Time = Time < 10 ? '0' + Time : Time;
					that.setData({
						Remaining: day + "天" + Time + "时" + Minute + "分" + second + "秒"
					});
					setTimeout(that.countdown, 1000);
				} else {
					that.setData({
						Remaining: '0'
					});
				}
			},

			// 确定参团
			determine() {
				var that = this;
				var id = that.actid;
				var parent_id = that.order_id;
				uni.showLoading({
					title: '加载中...',
					// #ifndef MP-ALIPAY
					mask: true,
					// #endif
				});
				getApp().globalData.sendRequest({
					url: '/Group/judgeGroup',
					type: 'post',
					data: {
						parent_id: parent_id
					},
					success: function(res) {
						uni.hideLoading();

						if (res.status == 1) {
							uni.navigateTo({
								url: '../Group-purchase-details/Group-purchase-details?id=' + id +
									'&parent_id=' + parent_id
							});
						} else {
							uni.showToast({
								title: res.msg,
								icon: 'none'
							});
							return false;
						}
					}
				});
			}

		}
	};
</script>
<style>
	@import "./Collage.css";
</style>
